<template>
    <div>
        <el-dialog title="批量打币工具(ETH)" :visible.sync="dialogVisible" :close-on-click-modal="false" :show-close="false">
            <el-form ref="form" :model="formApply" label-width="80px" size="medium">
                <el-form-item label="转出地址">
                    <el-input v-model="formApply.from"></el-input>
                </el-form-item>
                <el-form-item label="秘钥">
                    <el-input type="password" v-model="formApply.privateKey"></el-input>
                </el-form-item>
                <el-form-item label="GasLimit">
                    <el-input v-model="formApply.gasLimit"></el-input>
                </el-form-item>
                <el-form-item label="GasPrice">
                    <el-input v-model="formApply.gasPrice"></el-input>
                </el-form-item>
                <el-form-item label="打币数量">
                    <el-input v-model="formApply.count"></el-input>
                </el-form-item>
                <el-form-item label="接收地址">
                    <el-input v-model="formApply.toAddress"></el-input>
                </el-form-item>
            </el-form>

            <template>
                <el-upload
                        ref="upload"
                        accept=".xls,.xlsx"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :on-change="upload"
                        :show-file-list="false"
                        :auto-upload="false"
                >
                    <el-button slot="trigger" size="mini" type="primary">选取文件</el-button>
                </el-upload>
                <el-input v-model="excelValue" size="mini" placeholder="请输入想要获取列的表头名称"></el-input>
            </template>

            <div slot="footer" class="dialog-footer">
                <el-button :disabled="disabled" type="primary" @click="onSubmit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import XLSX from 'xlsx'
    let api = '/manager/user_balance_record';



    export default {
        name: "Tool",
        data() {
            return {
                dialogVisible: true,
                formApply: {
                    from: "0xd94Eb5f78c5D4d3AE1dcA16C2e41da962729E822", // 转出地址
                    privateKey: '0xf67952b47c271369735f194c50dabb975e1d8b88a6b5b028f6bee6fbc7c08132',
                    gasLimit: 80000,
                    gasPrice: 1,
                    count: 0,
                },
                disabled:false,
                addressArray: ['0x869DcEffaBCCc8c7597382A88DCf62C78d0fE91c'],


                excelValue:"id",
                outputs: []
            }
        },
        mounted() {
            this.eth_util.createAccount()
        },
        methods: {
            onSubmit() {
                this.disabled = true;
                setTimeout( ()=> {
                    this.disabled = false;
                },3000);
                let array = []
                for(let i in this.addressArray) {
                    array.push({
                        toAddress: this.addressArray[i],
                        ...this.formApply
                    })
                }
                this.tool_eth_util.batchSendETHTx(this.formApply.from, array, (data, params) => {
                    console.log('data ==== ', data);
                    if(data) {
                        if(data.code == 0) {
                            alert('hash = ' + data.data)
                        }else {
                            alert('error = ' + data.message)
                        }
                    }
                });
                // this.tool_eth_util.sendETHTx(this.formApply, (data) => {
                //     console.log('data ==== ', data);
                // });
                // this.tool_eth_util.sendETHTx(this.formApply, (data) => {
                //     console.log('data ==== ', data);
                // });
                // this.$confirm('打币已完成，请勿重复发送，稍后请到链上确认!', '提示', {
                //     confirmButtonText: '确定',
                //     showCancelButton:false,
                //     closeOnClickModal:false,
                //     type: 'warning'
                // }).then(() => {
                //
                // }).catch(() => {
                //
                // });
            },





            upload(file,fileList){
                let files = {0:file.raw};
                this.readExcel(files);
            },
            readExcel(files) {//表格导入
                // const files = e.target.files;
                // console.log(files);
                if(files.length<=0){//如果没有文件名
                    return false;
                }else if(!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())){
                    this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
                    return false;
                }
                const fileReader = new FileReader();
                fileReader.onload = (ev) => {
                    try {
                        const data = ev.target.result;
                        const workbook = XLSX.read(data, {
                            type: 'binary'
                        });
                        const wsname = workbook.SheetNames[0];//取第一张表
                        const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]);//生成json表格内容
                        // console.log(ws);
                        this.outputs = [];//清空接收数据
                        // 对数据进行处理成自己想要的
                        for(let i= 0;i<ws.length;i++){
                            this.outputs.push(ws[i][this.excelValue]);
                        }
                        this.$refs.upload.value = '';
                        console.log(this.outputs)
                    } catch (e) {
                        return false;
                    }
                };
                fileReader.readAsBinaryString(files[0]);
            }


        },

    }
</script>

<style scoped>

</style>

